package android.se1;

import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import android.app.Activity;
import android.os.Bundle;
import android.os.StrictMode;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;

public class SecondActivity extends Activity {

	EditText name, email, pw, repw, securityanswer;
	TextView error;
	Spinner securityquestion;
	Button signup;
	int match;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
				.detectDiskReads().detectDiskWrites().detectNetwork() // or .detectAll() for all detectable problems
				.penaltyLog().build());
		super.onCreate(savedInstanceState);
		setContentView(R.layout.register);
		
		name = (EditText) findViewById(R.id.editText1);
		email = (EditText) findViewById(R.id.editText2);
		pw = (EditText) findViewById(R.id.editText3);
		repw = (EditText) findViewById(R.id.editText4);
		signup = (Button) findViewById(R.id.buttonSignup);
		error = (TextView) findViewById(R.id.error2);
		//seca1 = (EditText) findViewById(R.id.editText5);
		//seca2 = (EditText) findViewById(R.id.editText6);		
		securityquestion = (Spinner) findViewById(R.id.spinner1);
		securityanswer = (EditText) findViewById(R.id.editText5);

		final AlertDialog alertDialog = new AlertDialog.Builder(this).create();

		signup.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {

				// TODO Auto-generated method stub

				ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();

				String mavs = "@mavs.uta.edu";
				String email_address = email.getText().toString();
				int check;
				check = email_address.indexOf(mavs);
				String name1 = name.getText().toString();
				String hometown = securityanswer.getText().toString();
				
				Pattern pattern = Pattern.compile("[a-z,A-Z, ]*");
				Matcher matcher = pattern.matcher(name1);
				
				Pattern securityanswerpattern = Pattern.compile("[a-z,A-Z, ]*");
				Matcher securityanswermatcher = securityanswerpattern.matcher(hometown);
				
				String password = pw.getText().toString();
				String retype_password = repw.getText().toString();

				if (password.equals(retype_password)) {
					match = 0;
				} else {
					match = -1;
				}
				
				if(!matcher.matches())
				{
					
					//Toast.makeText(SecondActivity.this,"Invalid Name. Please type again.", Toast.LENGTH_LONG).show();
					
					alertDialog.setTitle("Registration");
					alertDialog
							.setMessage("Invalid Name. Please type again.");
					alertDialog.setButton("OK",
							new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dialog,
										int type) {
									// here you can add functions
								}
							});
					alertDialog.show();
				}				
				else if (check == -1) {
					// error.setText("Invalid Email Address! Please Type Valid MAVS Email Address.");

					alertDialog.setTitle("Registration");
					alertDialog
							.setMessage("Invalid Email Address! Please Type Valid MAVS Email Address.");
					alertDialog.setButton("OK",
							new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dialog,
										int type) {
									// here you can add functions
								}
							});
					alertDialog.show();
				}else if(pw.getText().toString().length()<6)
				{
					
					alertDialog
							.setMessage("Password should be atleast 6 characters. Please type again.");
					alertDialog.setButton("OK",
							new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dialog,
										int type) {
									// here you can add functions
								}
							});
					alertDialog.show();
				}				
				else if (match == -1) {
					alertDialog.setTitle("Registration");
					alertDialog
							.setMessage("Password does not match. Please retype password.");
					alertDialog.setButton("OK",
							new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dialog,
										int type) {
									// here you can add functions
								}
							});
					alertDialog.show();
				}
				else if(!securityanswermatcher.matches())
				{
					
					//Toast.makeText(SecondActivity.this,"Invalid Name. Please type again.", Toast.LENGTH_LONG).show();
					
					alertDialog.setTitle("Registration");
					alertDialog
							.setMessage("Invalid Hometown. Please type again.");
					alertDialog.setButton("OK",
							new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dialog,
										int type) {
									// here you can add functions
								}
							});
					alertDialog.show();
				}					
				else {
					postParameters.add(new BasicNameValuePair("name", name
							.getText().toString()));
					postParameters.add(new BasicNameValuePair("email", email
							.getText().toString()));
					postParameters.add(new BasicNameValuePair("password", pw
							.getText().toString()));
					//postParameters.add(new BasicNameValuePair("securityans1", seca1.getText().toString()));
					postParameters.add(new BasicNameValuePair("securityquestion", securityquestion.getSelectedItem().toString())); 
					postParameters.add(new BasicNameValuePair("securityanswer", securityanswer.getText().toString()));

					String response = null;

					try {

						response = CustomHttpClient.executeHttpPost(
								//"http://192.168.1.145/CSE5324/signup.php",
								//"http://129.107.149.180/CSE5324/signup.php",
								"http://omega.uta.edu/~nxk0518/CSE5324/signup.php",
								//"http://192.168.0.3/mavbulletin/signup.php",
								postParameters);

						String result = response.toString();

						result = result.replaceAll("\\s+", "");

						if (result.equals("1")) {
							System.out.println("Result =" + result);
							// error.setText("Registration Successful");
							alertDialog.setTitle("Registration");
							alertDialog.setMessage("Registration Successful");
							alertDialog.setButton("OK",
									new DialogInterface.OnClickListener() {
										public void onClick(
												DialogInterface dialog,
												int type) {
											// here you can add functions
											Intent signinScreen = new Intent(
													getApplicationContext(),
													MavBulletinActivity.class);
											startActivity(signinScreen);
										}
									});

							alertDialog.show();

						} else {
							System.out.println("Result =" + result);
							// error.setText("Error in Saving Information!");
							alertDialog.setTitle("Registration");
							alertDialog
									.setMessage("Error in Saving Information! Email Address Already Exists.");
							alertDialog.setButton("OK",
									new DialogInterface.OnClickListener() {
										public void onClick(
												DialogInterface dialog,
												int type) {
											// here you can add functions
										}
									});

							alertDialog.show();
						}
					} catch (Exception e) {
						Log.e("log_tag",
								"Error in http connection!!" + e.toString());
						//email.setText(e.toString());
					}

				}

			}

		});

	}
}